package com.shuyihui.identity

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.shuyihui.identity.bean.Spec
import com.shuyihui.identity.bean.SpecCombination
import com.shuyihui.identity.databinding.ActivitySkuBinding

class SkuActivity : AppCompatActivity() {
    private val mBinding by lazy { ActivitySkuBinding.inflate(layoutInflater) }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(mBinding.root)

        // 规格列表
        val specList = listOf(
            Spec("颜色", listOf("紫色", "红色")),
            Spec("套餐", listOf("套餐一", "套餐二")),
            Spec("内存", listOf("64g", "128g", "256g"))
        )
        // 可供选择的规格组合
        val specCombinationList = listOf(
            SpecCombination("1", listOf("紫色", "套餐一", "64g")),
            SpecCombination("2", listOf("紫色", "套餐一", "128g")),
            SpecCombination("3", listOf("紫色", "套餐二", "128g")),
            SpecCombination("4", listOf("红色", "套餐二", "256g"))
        )
        val specAdapter = SpecAdapter(specList, specCombinationList, SpecSkuUtil.transformMatrix(specList, specCombinationList))
        specAdapter.setOnClickSpecListener(object : SpecAdapter.OnClickSpecListener {
            override fun clickSpec(enabled: Boolean, id: String?) {
                mBinding.btnSubmit.isEnabled = enabled
                if (enabled)
                    Toast.makeText(this@SkuActivity, "规格id=$id", Toast.LENGTH_SHORT).show()
            }

        })
        mBinding.rvSpec.layoutManager = LinearLayoutManager(this)
        mBinding.rvSpec2.layoutManager = LinearLayoutManager(this)
        mBinding.rvSpec.adapter = specAdapter

        val specList2 = listOf(
            Spec("头部", listOf("紫色", "红色")),
            Spec("身体", listOf("黑色", "绿色", "紫色")),
            Spec("腿部", listOf("紫色", "粉色")),
            Spec("鞋子", listOf("回力", "飞跃"))
        )
        val specCombinationList2 = listOf(
            SpecCombination("1", listOf("紫色", "黑色", "紫色", "回力")),
            SpecCombination("2", listOf("紫色", "紫色", "紫色", "飞跃")),
            SpecCombination("3", listOf("红色", "黑色", "粉色", "飞跃"))
        )
        val matrix = SpecSkuUtil.transformMatrix(specList2, specCombinationList2)
        for (one in matrix) {
            val temp = mutableListOf<Int>()
            for (two in one) {
                temp.add(two)
            }
            Log.v("Loren", "$temp")
        }
        val specAdapter2 = SpecAdapter(specList2, specCombinationList2, matrix)
        specAdapter2.setOnClickSpecListener(object : SpecAdapter.OnClickSpecListener {
            override fun clickSpec(enabled: Boolean, id: String?) {
                if (enabled)
                    Toast.makeText(this@SkuActivity, "规格id=$id", Toast.LENGTH_SHORT).show()
            }

        })
        mBinding.rvSpec2.adapter = specAdapter2
    }
}



